# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver import ChromeOptions
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
import requests
from config import url,proxy,is_window,sleep_time
from time import sleep

# 获取浏览器对象
def get_browser():
   option = ChromeOptions()
   #  关闭自动控制  
   option.add_experimental_option('excludeSwitches', ['enable-automation', 'enable-logging'])
   option.add_experimental_option('useAutomationExtension', False)
   #  代理IP
   BG_proxy = requests.get(proxy).text
   option.add_argument("--proxy-server=" + BG_proxy)
   #  是否显示浏览器
   if not is_window:
     option.add_argument('--headless')
     option.add_argument('--disable-gpu')

   return option
  


#  干掉全局变量
web = webdriver.Chrome(options=get_browser())
web.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
      'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'
      })
web.get('http://icanhazip.com')
sleep(3)
web.quit()

web1 = webdriver.Chrome(options=get_browser())
web1.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
      'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'
      })
web1.get('http://icanhazip.com')
sleep(3)
web1.quit()

web2 = webdriver.Chrome(options=get_browser())
web2.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
      'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'
      })
web2.get('http://icanhazip.com')
sleep(3)
web2.quit()


web3 = webdriver.Chrome(options=get_browser())
web3.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
      'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'
      })
web3.get('http://icanhazip.com')
sleep(3)
web3.quit()